package cn.com.lyj6851.user.service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;

import cn.com.lyj6851.user.entity.Role;
import cn.com.lyj6851.user.entity.SysUser;
import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class SsoUserDetailsService implements UserDetailsService {

	@Autowired
    private UserService userService; //用户
    @Autowired
    private RoleService roleService; //角色
    //@Autowired
    //private PermissionService permissionService; //权限

    private ObjectMapper objectMapper = new ObjectMapper(); //JSON转换


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    	boolean enabled = true; // 可用性 :true:可用 false:不可用
        boolean accountNonExpired = true; // 过期性 :true:没过期 false:过期
        boolean credentialsNonExpired = true; // 有效性 :true:凭证有效 false:凭证无效
        boolean accountNonLocked = true; // 锁定性 :true:未锁定 false:已锁定
       
        Set<GrantedAuthority> grantedAuthorities = new HashSet<>();
        
        SysUser userResult = userService.getOne(new QueryWrapper<>(new SysUser()).eq("username", username).or().eq("mobile_phone", username));
        if(userResult == null) {
        	throw new UsernameNotFoundException("用户:" + username + ",不存在!");
        }
        // 根据用户id获取角色
        List<Role> roleList = roleService.getRoleByUserId(userResult.getId());
        if(!roleList.isEmpty()) {
        	for(Role role:roleList) {
        		//角色必须是ROLE_开头，可以在数据库中设置
                GrantedAuthority grantedAuthority = new SimpleGrantedAuthority("ROLE_"+role.getName());
                grantedAuthorities.add(grantedAuthority);
                // 根据角色id获取权限内的菜单
                
        	}
        }
        
        User authUser = new User(username,userResult.getPassword(),
        		enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, grantedAuthorities);
        return authUser;
        /*
    	String password = PasswordEncoderFactories.createDelegatingPasswordEncoder().encode("123456");
        log.info("用户名 {}，数据库密码{}", username, password);
        User admin = new User(username,
                password,
                true, true, true, true,
                AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER"));
        return admin;*/
    }
    
  
}